package com.example.wanandroid.ui.activity;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toolbar;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.BaseResponse;
import com.example.wanandroid.core.bean.main.collect.FeedArticleListData;
import com.example.wanandroid.core.bean.project.ProjectListData;
import com.example.wanandroid.core.http.api.Apis;
import com.example.wanandroid.core.utils.HttpUtil;
import com.example.wanandroid.core.utils.StatusBarUtils;
import com.example.wanandroid.ui.adapter.ArticleListAdapter;
import com.example.wanandroid.ui.adapter.SearchResListAdapter;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchResultActivity extends AppCompatActivity {

    String input;
    SearchResListAdapter searchResListAdapter;
    LinearLayoutManager layoutManager;
    RecyclerView recyclerView;
    Toolbar mToolBar;
    RefreshLayout refreshLayout;
    Apis api;
    int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        Intent intent = getIntent();
        input = intent.getStringExtra("input");
        recyclerView = findViewById(R.id.search_res_list);
        mToolBar = findViewById(R.id.search_detail_toolbar);
        mToolBar.setTitle(input + ": 搜索结果");

        layoutManager = new LinearLayoutManager(SearchResultActivity.this);
        searchResListAdapter = new SearchResListAdapter(SearchResultActivity.this);
        recyclerView.setAdapter(searchResListAdapter);
        recyclerView.setLayoutManager(layoutManager);
        page=1;

        refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(SearchResultActivity.this));
        refreshLayout.setRefreshFooter(new BallPulseFooter(SearchResultActivity.this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            initView();
            refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            updateData();
            refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
        });

        initView();
    }

    private void initView() {
        //创建网络请求接口对象实例
        api = HttpUtil.getApi();
        //对发送请求进行封装，传入接口参数
        Call<BaseResponse<FeedArticleListData>> searchList = api.getSearchList(0, input);

        searchList.enqueue(new Callback<BaseResponse<FeedArticleListData>>() {
            @Override
            public void onResponse(Call<BaseResponse<FeedArticleListData>> call, Response<BaseResponse<FeedArticleListData>> response) {
                FeedArticleListData feedArticleListData = response.body().getData();
                List<BaseData> feedArticleDataList = new ArrayList<>(feedArticleListData.getDatas());
                searchResListAdapter.addDataList(feedArticleDataList);
            }

            @Override
            public void onFailure(Call<BaseResponse<FeedArticleListData>> call, Throwable t) {

            }
        });
    }

    private void updateData(){
        Call<BaseResponse<FeedArticleListData>> searchList = api.getSearchList(page,input);

        searchList.enqueue(new Callback<BaseResponse<FeedArticleListData>>() {
            @Override
            public void onResponse(Call<BaseResponse<FeedArticleListData>> call, Response<BaseResponse<FeedArticleListData>> response) {
                FeedArticleListData feedArticleListData = response.body().getData();
                List<BaseData> feedArticleDataList = new ArrayList<>(feedArticleListData.getDatas());
                searchResListAdapter.addDataList(feedArticleDataList);
                page++;
            }

            @Override
            public void onFailure(Call<BaseResponse<FeedArticleListData>> call, Throwable t) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUtils.setStatusBar(this);
    }
}